package com.uchengwang.ui.utils;

import android.app.Activity;
import android.content.Intent;

import com.uchengwang.R;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.login.ui.LoginActivity;
import com.uchengwang.ui.cm_ui.umsg.ui.ClubMainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/7/19.
 */

public class ActivityUtils {
    private static ActivityUtils utils;
    private ActivityUtils(){}

    public static ActivityUtils getUtils() {
        if (utils ==null){
            utils = new ActivityUtils();
        }
        return utils;
    }

    /**
     * 将MainActivity清除
     * */
    public void clearMainActivity(){
        List<Activity> list = UChengApplication.instance.getList();
        if (list != null && list.size() >0){
            for (Activity activity :list){
                if (activity != null && activity instanceof Activity){
                    if (activity instanceof MainActivity  && activity != null){
                        activity.finish();
                        list.remove(activity);
                        return;
                    }
                }

            }
        }
    }
    /**
     * 将ClubMainActivity清除
     * */
    public void clearClubMainActivity(){
        List<Activity> list = UChengApplication.instance.getList();
        if (list != null && list.size() >0){
            for (Activity activity :list){
                if (activity != null && activity instanceof Activity){
                    if (activity instanceof ClubMainActivity && activity != null){
                        activity.finish();
                        list.remove(activity);
                        return;
                    }
                }

            }
        }
    }

    /**
     * 将除了 LoginActivity的activity清除
    * */
    public void overLoginActivity(){
        ExecutorsUtils.getUtils().createSingleThread().execute(new Runnable() {
            @Override
            public void run() {
                List<Activity> list = UChengApplication.instance.getList();
                if (list != null && list.size() >0){
                    List<Activity> removes = new ArrayList<>();
                    for (Activity activity :list){
                        if (activity != null && activity instanceof Activity){
                            if (!(activity instanceof LoginActivity)){
                                removes.add(activity);
                                activity.finish();
                            }
                        }
                    }

                    list.removeAll(removes);
                }
            }
        });
    }

    /**
     * activity的打开方式
     * 从左到有打开
     * */
    public void leftToRight(Activity activity){
        clearMainActivity();
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }
    /**
     * activity的打开方式
     * 从左到有打开
     * */
    public void rightToLeft(Activity activity){
        clearMainActivity();
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);

    }


}
